﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoVuaClient
{
    class BanCo
    {
        private OCo[,] dsOCo;
        public BanCo()
        {            
            DsOCo = new OCo[8,8];
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    DsOCo[i, j] = new OCo(j, i);
                }
            }
        }

        public OCo[,] DsOCo
        {
            get { return dsOCo; }
            set { dsOCo = value; }
        }
        public void DatQuanCoLenBanCo()
        {
            foreach (var oCo in dsOCo)
            {
                oCo.DatQuanCoLenOCo();
            }
        }
        public List<OCo> NhungOCoTheDi(int chiSoCot, int ChiSoDong)
        {
            return DsOCo[ChiSoDong, chiSoCot].NhungOCoTheDi(this, false);
        }

        public QuanCo DiChuyenQuanCo(int chiSoCotHT, int chiSoDongHT, int chiSoCotDen, int chiSoDongDen)
        {
            OCo oHienTai = DsOCo[chiSoCotHT, chiSoDongHT];
            OCo oCanDen = DsOCo[chiSoCotDen, chiSoDongDen];
            if (oCanDen.QuanCoHienTai == null)
            {
                DsOCo[chiSoCotDen, chiSoDongDen].QuanCoHienTai = DsOCo[chiSoCotHT, chiSoDongHT].QuanCoHienTai;
                DsOCo[chiSoCotDen, chiSoDongDen].QuanCoHienTai.DaXuatQuan();
                DsOCo[chiSoCotHT, chiSoDongHT].QuanCoHienTai = null;
                return null;
            }
            else
            {
                QuanCo quanCo = new QuanCo();
                quanCo = oCanDen.QuanCoHienTai;
                DsOCo[chiSoCotDen, chiSoDongDen].QuanCoHienTai = oHienTai.QuanCoHienTai;
                DsOCo[chiSoCotDen, chiSoDongDen].QuanCoHienTai.DaXuatQuan();
                DsOCo[chiSoCotHT, chiSoDongHT].QuanCoHienTai = null;             
                return quanCo;
            }
        }
        public bool KiemTraCoDiChuyenKo(int chiSoCotHT, int chiSoDongHT, int chiSoCotDen, int chiSoDongDen)
        {
            if (chiSoCotHT == chiSoCotDen && chiSoDongHT == chiSoDongDen)
            {
                return false;
            }
            if (DsOCo[chiSoCotHT, chiSoDongHT].QuanCoHienTai != null)
            {
                foreach (var oCo in DsOCo[chiSoCotHT, chiSoDongHT].NhungOCoTheDi(this, false))
                {
                    if (oCo.Cot == DsOCo[chiSoCotDen, chiSoDongDen].Cot && oCo.Dong == DsOCo[chiSoCotDen, chiSoDongDen].Dong)
                    {
                        return true;
                    }
                }
            }
            
            return false;
        }

        public bool KiemTraChieuTuong(int chiSoCot, int chiSoDong)
        {
            foreach (var oCo in DsOCo[chiSoCot,chiSoDong].NhungOCoTheDi(this, false))
            {
                if (oCo.QuanCoHienTai != null)
                {
                        return true;
                }
            }
            return false;
        }
    }
}
